using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace FTP_Upload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                txtFilePath.Text = openFileDialog1.FileName.ToString().Trim();
            }
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            if (txtFilePath.Text.Length == 0)
            {
                MessageBox.Show("Please select file first!!");
                btnBrowse.Focus();
                return;
            }

            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
                txtFtpServer.Text.Trim() + "/" +
                Path.GetFileName(txtFilePath.Text.Trim()));

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Text);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;
//          [10:00] puguhhg_1311: ftp
//          [10:00] puguhhg_1311: host : 172.16.15.45 
//          [10:01] puguhhg_1311: user : kucing
//          [10:01] puguhhg_1311: pass : kucing

            FileStream stream = File.OpenRead(txtFilePath.Text.Trim());
            byte[] buffer = new byte[stream.Length];


            stream.Read(buffer, 0, buffer.Length);
            stream.Close();


            Stream reqStream = request.GetRequestStream();
            reqStream.Write(buffer, 0, buffer.Length);
            reqStream.Close();

            MessageBox.Show("Sukses coy");
        }
    }
}